package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.PageInfoToPageUtils;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author ljj
 * @create 2022-11-16-20:39
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping
    public R<String> save(@RequestBody  DishDto dishDto){
        log.info("dishDto:{}",dishDto);
        dishService.saveWithFloavor(dishDto);
        return R.success("ok");

    }

    //@GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        Page<Dish> objectPage = new Page<>(page, pageSize);
        Page<DishDto> objectPage2 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper
                .like(name!=null,Dish::getName,name)
                .orderByDesc(Dish::getUpdateTime);
        Page<Dish> page1 = dishService.page(objectPage, dishLambdaQueryWrapper);
        BeanUtils.copyProperties(objectPage,objectPage2,"records");
        List<Dish> records = objectPage.getRecords();


        List<DishDto> list= records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto );
            Long categoryId = item.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            System.out.println();
            dishDto.setCategoryName(byId.getName());
            return dishDto;
        }).collect(Collectors.toList());
        objectPage2.setRecords(list);

        return R.success(objectPage2);

    }
    @GetMapping("/page")
    public R<Page> page2(int page, int pageSize, String name){

        PageHelper.startPage(page,pageSize);
        List<DishDto> dishInfo = dishService.getDishInfo(name);
        PageInfo<DishDto> objectPageInfo = new PageInfo<>(dishInfo);
        Page<DishDto> tranto = PageInfoToPageUtils.tranto(objectPageInfo);
        return R.success(tranto);

    }
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto byIdWithFlavor = dishService.getByIdWithFlavor(id);

        return R.success(byIdWithFlavor);

    }

    @PutMapping
    public R<String> update(@RequestBody  DishDto dishDto){
        String key ="dish_"+dishDto.getCategoryId()+"_1" ;
        redisTemplate.delete(key);
        dishService.updateWithFlavor(dishDto);
        //Set keys = redisTemplate.keys("dish_*");
        //redisTemplate.delete(keys);
        return R.success("ok");

    }
    /*@GetMapping("/list")
    public R<List<Dish>> list(Dish dish){
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        dishLambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        List<Dish> list = dishService.list(dishLambdaQueryWrapper);

        return R.success(list);

    }*/

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        List<DishDto> dishInfo=null;
        String key ="dish_"+dish.getCategoryId()+"_" +dish.getStatus();
        dishInfo = (List<DishDto>)redisTemplate.opsForValue().get(key);
        if(dishInfo !=null){
            return R.success(dishInfo);
        }
        dishInfo = dishService.getDishDtoInfoByDish(dish);
        redisTemplate.opsForValue().set(key,dishInfo,5, TimeUnit.MINUTES);
        return R.success(dishInfo);

    }
}
